package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.Patients;
import cn.sxt.clinic.pojo.PatientsCases;
import cn.sxt.clinic.pojo.ResultCode;
import cn.sxt.clinic.service.IPatientsCasesService;
import cn.sxt.clinic.service.IPatientsService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-07-02
 */
@Controller
@RequestMapping("/patients")
public class PatientsController {

    @Autowired
    private IPatientsService patientsService;

    @Autowired
    private IPatientsCasesService patientsCasesService;

    @RequiresPermissions("patients:patientsManager")
    @RequestMapping("/patientsManager")
    public String patientsManager(){
        return "patientsManager";
    }

    @RequiresPermissions("patients:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1")Integer pageNum,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   String keyword){
        IPage<Patients> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Patients> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("patients_name",keyword);
        }

        IPage<Patients> reslut = patientsService.page(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",reslut.getTotal());
        map.put("rows",reslut.getRecords());
        return map;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public Map<String,Object> detailed(Long patientsId){
        IPage<PatientsCases> page = new Page<>(1,10);
        QueryWrapper<PatientsCases> wrapper = new QueryWrapper<>();
        wrapper.eq("patients_id",patientsId);
        IPage<PatientsCases> result = patientsCasesService.page(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows", result.getRecords());
        return map;
    }

    @RequiresPermissions("patients:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delete(Long patientsId){
        boolean b = patientsService.removeById(patientsId);
        ResultCode resultCode=null;
        if (b){
            resultCode=new ResultCode(1,"删除成功");
        }else{
            resultCode=new ResultCode(0,"删除失败");
        }
        return resultCode;
    }

}

